package com.hugoo.core.db.rp;

import com.hugoo.core.db.mo.DCV;
import com.hugoo.core.util.TL;
import com.hugoo.rpt.ExpPageInfo;
import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 *
 * @author Lovelock.Chan
 */
public abstract class ResultPack implements com.hugoo.core.db.ResultPack {

    protected final Log log = LogFactory.getLog(this.getClass());
    protected final ExpPageInfo pi;
    protected final String col[][];
    private final NumberFormat ccn = DecimalFormat.getNumberInstance();

    {
        ccn.setMaximumFractionDigits(2);
        ccn.setGroupingUsed(false);
    }

    public ResultPack(ExpPageInfo pi, String[][] col) {
        this.pi = pi;
        this.col = col;
    }

    protected String getValue(ResultSet rs, String key) {
        return DCV.getValue(rs, key);
    }

    protected Double getDoubleValue(ResultSet rs, String key) {
        Object k = null;
        try {
            k = rs.getObject(key.toUpperCase());
        } catch (SQLException ex) {
            log.warn("读取字段【" + key + "】值时，出现异常！", ex);
        }
        if (k == null) {
            return null;
        }
        if (k instanceof BigDecimal) {
            return ((BigDecimal) k).doubleValue();
        } else if (k instanceof String) {
            return TL.parseDouble(k.toString());
        }
        log.info("无效的数值类型！");
        return null;
    }
}
